//
//  YD_GetLocationTool.m
//  SpeedDating
//
//  Created by isu on 17/1/5.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "FriendGetLocationTool.h"
#import <CoreLocation/CoreLocation.h>
@interface FriendGetLocationTool() <CLLocationManagerDelegate> {
    CLLocationManager *_locationManager;
    CLGeocoder *_geoC;
}
@end
@implementation FriendGetLocationTool
-(void)getCurrentLocation{
    _geoC = [[CLGeocoder alloc]init];
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    [_locationManager requestAlwaysAuthorization];
    [_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [_geoC reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(error == nil)
        {
            NSLog(@"%@", placemarks);
            for (CLPlacemark *placeMark in placemarks) {
                NSDictionary *addressDic = placeMark.addressDictionary;
                NSString *state=[addressDic objectForKey:@"State"];
                NSString *city=[addressDic objectForKey:@"City"];
                NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
                NSString *street=[addressDic objectForKey:@"Street"];
                NSString *str = [NSString stringWithFormat:@"%@%@%@",city,subLocality,street];
                //NSLog(@"所在城市====%@ %@ %@ %@", state, city, subLocality, street);
                self.block(newLocation,str);
            }
            
        }else
        {
            NSLog(@"cuowu");
        }
    }];
    [manager stopUpdatingLocation];
}

@end
